package today.rocky.boot.cloud.nacos.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import today.rocky.boot.cloud.nacos.config.BlockHandler;
import today.rocky.boot.cloud.nacos.feign.ProFeign;

import java.util.concurrent.TimeUnit;

/**
 * @author Rocky
 * @date 2020/2/25 17:46
 * @RefreshScope 支持nacos动态刷新
 */
@RestController
@CrossOrigin
@RefreshScope
public class ConsumeController {

    @Autowired
    private ProFeign feign;

    @Value("${config.info}")
    private String config;

    @Value("${cjy.love}")
    private String love;

    @GetMapping("/conf")
    public String getConfig(){
        return config;
    }

    @GetMapping("/love")
    public String getLove(){
        return love;
    }

    @GetMapping("/fuse")

    public String getFuse() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        return "fuse is melting";
    }

    @GetMapping("/hello")
    public String sayHello(){
        return feign.sayhello()+"黑凤梨，希望天天天在一起";
    }


    @GetMapping("hotKey")
    @SentinelResource(value = "hotKey" ,blockHandler = "dealHotKey")
    public String hotKey(@RequestParam(required = false) String p1, @RequestParam(required = false)  String p2){
        return "hot key -------";
    }
    //兜底方法
    public String dealHotKey(String p1, String p2,BlockException e){
        return "hot key -----is gone";
    }

    @GetMapping("/limit/handClzExp")
    @SentinelResource(value = "handClzExp" ,blockHandlerClass = BlockHandler.class,blockHandler = "handlerException")
    public String handExpClazz(){
        return "mei you yi chang fa sheng";
    }


}
